using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace SBPweb.Logging
{
	
	/// <summary>
	/// Provides additional Logging options for configuration file
	/// </summary>
	public class LoggingOptionsSection : ConfigurationSection
	{
		private Dictionary<string, SeverityLevel> mSeverityFilters = new Dictionary<string, SeverityLevel>();

        /// <summary>
		/// Initializes a new LoggingOptionsSection instance
		/// </summary>
		public LoggingOptionsSection()
        {
        }

		/// <summary>
		/// Gets or sets true if global implicit categories are disabled
		/// </summary>
		[ConfigurationProperty("disableGlobalImplicitCategories", IsRequired = false, DefaultValue = false)]
		public Boolean DisableGlobalImplicitCategories
		{
			get
			{
				return (Boolean)this["disableGlobalImplicitCategories"];
			}
			set
			{
				this["disableGlobalImplicitCategories"] = value;
			}
		}

		/// <summary>
		/// Gets or sets a KeyValueConfigurationCollection instance that contains the severity filters for catgories
		/// </summary>
		[ConfigurationProperty("severityFilters", IsRequired = true)]
		public KeyValueConfigurationCollection SeverityFilters
		{
			get
			{
				return this["severityFilters"] as KeyValueConfigurationCollection;
			}
			set
			{
				this["severityFilters"] = value;
			}
		}

		/// <summary>
		/// Applies severity filters for categories
		/// </summary>
		public void ApplySeverityFilters()
		{
			foreach (string key in SeverityFilters.AllKeys)
			{
				string[] split = SeverityFilters[key].Value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
				SeverityLevel level = SeverityLevel.None;
				foreach (string l in split)
				{
					level |= (SeverityLevel)Enum.Parse(typeof(SeverityLevel), l.Trim());
				}

				mSeverityFilters.Add(key, level);
			}

			foreach (string key in mSeverityFilters.Keys)
			{
				LogManager.SetCategorySeverityFilter(key, mSeverityFilters[key]);
			}
		}

		/// <summary>
		/// Uninitializes LoggingOptionsSection instance
		/// </summary>
		public void Terminate()
		{
			mSeverityFilters.Clear();
		}
	}
}
